package popple.core
{

	import flash.display.Stage;
	import flash.display.Stage3D;
	import flash.display3D.Context3D;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormatAlign;
	import flash.ui.Keyboard;
	import flash.utils.getTimer;
	
	import popple.cameras.Camera;
	import popple.display.DisplayObject;
	import popple.display.DisplayObjectContainer;
	import popple.display.Stage;
	import popple.events.EngineEvent;
	

	/**
	 * 引擎引导类,主导系统初始化.启动Stage 
	 * @author popplecui
	 * 
	 */	
	public class PP2D extends EventDispatcher
	{
		static public var stage:flash.display.Stage;
		static private var _stageUsed:uint;
		private var _stage3D:Stage3D;
		private var _root:popple.display.Stage;
		public var context3d:Context3D;
		private var _rootClass:Class;
		
		private var _renderObjects:Array;
		private var _camera:Camera;
		
		private var _info:TextField;
		private var _screenInput:TextField;
		private var _viewPort:Rectangle;
		/**
		 * 
		 * @param root
		 * @param fstage
		 * @param viewPort
		 * @example
		 <listing version="3.0">
		 var pp2d:PP2D=new PP2D(MyStage,stage,new Rectangle(0,0,960,560));
		 pp2d.start();
		 </listing>
		 */			
		
		public function PP2D(root:Class,fstage:flash.display.Stage,viewPort:Rectangle)
		{
			
			_viewPort=viewPort;
			
			_info=createInfoText();
			_info.text="分辨率:"+_viewPort.width+"*"+_viewPort.height;
			_info.addEventListener(MouseEvent.CLICK,onChangeInput);
			_info.selectable=false;
			stage=fstage;
			stage.addChild(_info);
			
			setViewPort(_viewPort)
			_rootClass=root;
			_camera=new Camera;
			_camera.active=true;
			
			
			
			_stage3D=stage.stage3Ds[_stageUsed];
			_stage3D.addEventListener(Event.CONTEXT3D_CREATE,onStage3DCreate);
			_stageUsed++;
			
			
			setViewPort(_viewPort)
		}
		private function setViewPort(port:Rectangle):void
		{
			popple.display.Stage.width=port.width;
			popple.display.Stage.height=port.height;
			popple.display.Stage.scale=port.height/port.width;
			_info.text="分辨率:"+port.width+"*"+port.height;
			
			
			
		}
		private function onChangeInput(e:MouseEvent):void
		{
			if(_screenInput==null)_screenInput=createInfoText(1);
			_screenInput.x=e.stageX;
			_screenInput.y=e.stageY+_screenInput.height;
			_screenInput.text="输入分辨率";
			_screenInput.addEventListener(MouseEvent.CLICK,onInput);
			_screenInput.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
			stage.addChild(_screenInput);
		}
		private function onKeyDown(e:KeyboardEvent):void
		{
			if(e.keyCode==Keyboard.ENTER)
			{
				var str:String=_screenInput.text;
				var w:Number=str.split("*")[0];
				var h:Number=str.split("*")[1];
				setScreem(w,h);
				stage.removeChild(_screenInput);
			}
		}
		private function onInput(e:MouseEvent):void
		{
			_screenInput.text="";
		}
		/**
		 *调整屏幕分辨率 
		 * 
		 */		
		private function createInfoText(type:uint=0):TextField
		{
			var tf:TextField=new TextField;
			if(type==1){tf.type=TextFieldType.INPUT}
			    tf.autoSize=TextFormatAlign.LEFT;
				tf.wordWrap=true;
				tf.width=200;
				tf.filters=[new GlowFilter(0xffffff,1,2,2,1000)];
			return tf;
		}
		public function setScreem(width:Number,height:Number):void
		{
			setViewPort(new Rectangle(0,0,width,height));
		}
		private function onStage3DCreate(e:Event):void
		{
			context3d=_stage3D.context3D;
			
			RenderHelper.getInstance().initialize(context3d);
			buildStage();
		}
		public function start():void
		{
			_stage3D.requestContext3D();
		}
		private function buildStage():void
		{
			_root=(new _rootClass) as popple.display.Stage;
			_root.name="Stage";
			if(_root==null)
			{
				throw new Error("Root不是Stage子类");
			}
			_root.stage=_root;
			_root.dispatchEvent(new EngineEvent(EngineEvent.READY));
			/*context3d.clear();
			renderTree(_root);
			context3d.present();*/
			stage.addEventListener(Event.ENTER_FRAME,onStart);
		}
		private function onStart(e:Event):void
		{
			var t1:Number=getTimer();
			context3d.clear();
			renderTree(_root);
			context3d.present();
			var t2:Number=getTimer();
			
		}
		private function renderTree(root:DisplayObject):void
		{
			
			var obj:DisplayObject=root;
			
			if(obj is DisplayObjectContainer)
			{
				var children:Vector.<DisplayObject>=(obj as DisplayObjectContainer).getChildren();
				
				obj.render();
				
				
				for(var i:uint=0;i<children.length;i++)
				{
					obj=children[i] as DisplayObject;
					
					if(obj is DisplayObjectContainer)
					{
						renderTree(obj);
					}
					else
					{
						obj.render();
					}
				}
				
			}
			else
			{
				obj.render();
			}
		}
	}
}